const { Model, DataTypes } = require("sequelize");
const sequelize = require("../app/database");
const User = require("../model/user.model");

class Apply extends Model {}

Apply.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    thing_name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: User,
        key: "id",
      },
    },
    approval_status: {
      type: DataTypes.INTEGER,
    },
    thing_images: {
      type: DataTypes.STRING,
    },
    thing_video: {
      type: DataTypes.STRING,
    },
    certificate: {
      type: DataTypes.STRING,
    },
    thing_describe: {
      type: DataTypes.STRING,
    },
    types: {
      type: DataTypes.STRING,
    },
    createAt: {
      type: DataTypes.DATE,
    },
    updateAt: {
      type: DataTypes.DATE,
    },
  },
  {
    tableName: "apply",
    createdAt: "createAt",
    updatedAt: "updateAt",
    timestamps: true,
    sequelize,
  }
);

// 将两张表联系在一起
User.hasMany(Apply, {
  foreignKey: "user_id",
});
Apply.belongsTo(User, {
  foreignKey: "user_id",
});

module.exports = Apply;
